package penguin.masking;

import java.util.HashMap;
import java.util.Map;

/**
 * @author wuxh
 * @version 1.0.0
 */
public class ServiceProvider {

    private static final Map<String, Object> PROVIDER_MAP = new HashMap<>();

    public static final String dataReductionCache = "DataReductionCache";
    public static final String wordManager = "WordManager";

    private static final String[] PROVIDER_KEYS = new String[]{dataReductionCache, wordManager};

    static {
        init();
    }

    private static void init() {
        if (!PROVIDER_MAP.isEmpty()) {
            return;
        }
        for (String providerKey : PROVIDER_KEYS) {
            try {
                String cache = ContextConfigLoad.configLoad().getProperty(providerKey);
                Class<?> aClass = Class.forName(cache);
                PROVIDER_MAP.put(providerKey, aClass.newInstance());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static DataReductionCache reductionCache() {
        init();
        return (DataReductionCache) PROVIDER_MAP.get(dataReductionCache);
    }

    public static WordManager wordManager() {
        init();
        return (WordManager) PROVIDER_MAP.get(wordManager);
    }

    public static void clear() {
        PROVIDER_MAP.clear();
    }
}
